﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
public partial class Agency_TraCheckIsbidcompany_PSF : System.Web.UI.Page
{
    public string _strProjectId;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSupplyCompanyCheck);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSupervise);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            _strProjectId = SysFun.IsSQL(Request.QueryString["ProjectId"].ToString());
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (!IsPostBack)
            {
                bool flag=false; //是否十地市招标
                string strSql = "";
                if(_Account.Type=="14")
                {
                   strSql = "select * from area  where areaid >530000 and areaid<540000 and areaid%100=0  ";
                }
                else if (_Account.AreaId == "530000")   //招标中心
                {
                    strSql = "select * from area  where areaid >530000 and areaid<540000 and areaid%100=0  ";
                    Button1.Visible = true;//批量审核
                    Button2.Visible = true;//单个未审核
                    Button4.Visible = true;//单个审核
                }
                else                               //各地市招标办
                {
                    strSql = "select * from area  where areaid >530000 and areaid<540000 and areaid%100=0 and  areaid="+_Account.AreaId;
                    flag=true;
                }
                Db DbProject = new Db();
              
                DataTable dt = DbProject.DataTable(strSql);
                if (!flag)
                {
                    DataRow dw = dt.NewRow();
                    dw[0] = "0";
                    dw[1] = "全部";
                    dt.Rows.InsertAt(dw, 18);
                }
                lstAreaName.DataSource = dt;
                lstAreaName.DataTextField = "areaname";
                lstAreaName.DataValueField = "areaid";
                lstAreaName.DataBind();
                dt.Clear();
                DbProject.Close();
            }
            if (Request.Form["PurchaseChoose"] == "审核通过" && Request.Form["chk"] != null)
            {
                String StrSql = "", strid = "";
                
                strid = Request.Form["chk"];
                string[] SGID = strid.Length == 0 ? new string[] { "" } : strid.Substring(0).Split(new char[] { ',' });
                StrSql = "";
               
                for (int i = 0; i < SGID.Length; i++)
                {
                    StrSql += "@" + "update SupplyPlan_GoodsNext set  PS_ChangeStatus=1,Accountid_sh='" + SysFun.ToLong(_Account.AccountId) + "',Accountname_shr='" + _Account.AccountName + "' ,CheckStatusDatetime=getdate() where id=" + SGID[i].ToString();                
                }
                if (StrSql.Length > 0)
                {
                    Db db = new Db();
                    db.ExecuteTransaction(StrSql.Substring(1).Split(new char[] { '@' }), 30);
                    db.Close();
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "SupplyPlan_GoodsNext", StrSql, "../Agency/TraCheckIsbidcompany_PSF.aspx");
                   
                }
            }
            showGridView();
        }
    }


    public void showGridView()
    {
        int i = 0;
        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "SupplyPlan_Goods INNER JOIN SupplyPlan_GoodsNext ON SupplyPlan_Goods.SupplyGoodsId = SupplyPlan_GoodsNext.SupplyGoodsId AND SupplyPlan_Goods.AccountId_PS = SupplyPlan_GoodsNext.Accountid_ps_M";                                               //要查询的表明
        dbPage.Key = "SupplyPlan_GoodsNext.id";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "id,GoodsId,ProductName,MedicineModel,Outlookc,Factor,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels,UserCode_TB,CompanyName_TB,CompanyName_PS,areaname,companyname_ps_B1,case when SupplyPlan_GoodsNext.PS_ChangeStatus = '0' or SupplyPlan_GoodsNext.PS_ChangeStatus is null then '未审核'   when SupplyPlan_GoodsNext.PS_ChangeStatus ='1' then '审核通过' when SupplyPlan_GoodsNext.PS_ChangeStatus ='2' then '审核未通过' end as PS_ChangeStatus,SupplyPlan_GoodsNext.Accountname_shr,SupplyPlan_GoodsNext.NopssReason,case GoodsState when '0' then '原中标' when '1' then '自行采购' when '2' then '22种降价' when '3' then '血液制品' when '4' then '重点监控' end as goodsstate ";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 20;                                                    //没有显示记录条数   
        dbPage.OrderBy = "CompanyName_TB,productname,medicinemodel";//排序字段


        System.Text.StringBuilder strHtml = new System.Text.StringBuilder();
        strHtml.Append("<table width='98%' border='0' align='center' cellpadding='2' cellspacing='1' class='table_table'>");
        strHtml.Append("<tr>");
        strHtml.Append("<td class='table_title' nowrap>流水号</td>");
        strHtml.Append("<td class='table_title' nowrap>通用名</td>");
        strHtml.Append("<td class='table_title' nowrap>剂型</td>");
        strHtml.Append("<td class='table_title' nowrap>规格</td>");
        strHtml.Append("<td class='table_title' nowrap>转换系数</td>");
        strHtml.Append("<td class='table_title' nowrap>质量层次</td>");
        strHtml.Append("<td class='table_title' nowrap>中标人注册号</td>");
        strHtml.Append("<td class='table_title' nowrap>中标人名称</td>");
        strHtml.Append("<td class='table_title' nowrap>主配送企业</td>");
        strHtml.Append("<td class='table_title' nowrap>地区</td>");
        strHtml.Append("<td class='table_title' nowrap>分配送企业</td>");
        strHtml.Append("<td class='table_title' nowrap>中标状态</td>");
        strHtml.Append("<td class='table_title' nowrap>审核状态</td>");
        strHtml.Append("<td class='table_title' nowrap>审核人</td>");
        strHtml.Append("<td class='table_title' nowrap>审核</td>");
        strHtml.Append("<td class='table_title' nowrap>选择</td>");
        strHtml.Append("</tr>");
        System.Data.IDataReader dr = dbPage.ExecuteReader();

        while (dr.Read())
        {
            string _strCheckStatus = dr["PS_ChangeStatus"].ToString();
            if (_strCheckStatus == "2")
            {
                strHtml.Append("<tr style='background-color:Yellow'>");
            }
            else
            {
                if (i == 0)
                {
                    strHtml.Append("<tr class='table_td'>");
                    i++;
                }
                else
                {
                    strHtml.Append("<tr class='table_td2'>");
                    i--;
                }
            }

            strHtml.Append("<td>" + dr["goodsid"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["ProductName"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["MedicineModel"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["Outlookc"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["Factor"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["QualityLevels"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["UserCode_TB"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["CompanyName_TB"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["CompanyName_PS"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["areaname"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["companyname_ps_B1"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["goodsstate"].ToString() + "</td>");
            if (dr["PS_ChangeStatus"].ToString() == "审核未通过")
            {
                strHtml.Append("<td title='" + dr["NopssReason"].ToString() + "' style=' color: #ff0000;' >" + dr["PS_ChangeStatus"].ToString() + "</td>");
            }
            else
            {
                strHtml.Append("<td >" + dr["PS_ChangeStatus"].ToString() + "</td>");
            }
            strHtml.Append("<td>" + dr["Accountname_shr"].ToString() + "</td>");//审核人
            if (dr["CompanyName_PS"].ToString().Trim() == "")
            {
                strHtml.Append("<td>企业未维护</td>");
            }
            else
            {
                //strHtml.Append("<td><a target=\"_blank\" href=\"TraCheckIsbidcompany_PSFinfo.aspx?id='" + dr["id"].ToString() + "'\" onClick=\"window.open(this.href,'+''''+''''+','+''''+'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=1'+ ''''+');return false;\">审核</a></td>");
                strHtml.Append("<td><a target=\"_blank\" href=\"TraCheckIsbidcompany_PSFinfo.aspx?id=" + dr["id"].ToString() + "\" onClick=\"javascript:window.open(this.href,'');return false;\">审核</a></td>");
            }
            strHtml.Append("<td width='3%'><input name='chk' type='checkbox' value='" + dr["id"].ToString() + "' /></td>");
            strHtml.Append("</tr>");
        }
        strHtml.Append("</table>");
        Label1.Text = strHtml.ToString();
        //BId = Request.Form["bidid"].ToString();
        //GridView1.DataSource = dr;
        //GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();

        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键



    }

    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp, CheckStatus,GoodsStatus,  _ps, _GoodsId, name, outlookc, _companyname_tb, _usecoder_tb,_fps;

        name = ProductName.Text.ToString();
        outlookc = Outlookc.Text.ToString();

        _companyname_tb = Textcompanyname_tb.Text.Trim();
        _usecoder_tb = txtCompanyUserCode.Text.Trim();
       // strtemp = " projectid=" + _strProjectId + "  and SupplyPlan_Goods.isuse=1 and";
        strtemp = " SupplyPlan_Goods.isuse=1 and";
        _ps = SysFun.ToTrim(txtps.Text);
        _GoodsId = SysFun.ToTrim(txtGoodsId.Text);
        _fps = txtFpsr.Text; //DropDownList1

        //中标状态
        GoodsStatus = DropDownList1.SelectedValue;
        switch (GoodsStatus)
        {
            case "5":  //全部
                break;
            case "0"://原中标
                strtemp += " goodsstate=0 and";
                break;
            case "1"://自行采购
                strtemp += " goodsstate=1 and";
                break;
            case "2"://22种降价
                strtemp += " goodsstate=2 and";
                break;
            case "3"://血液制品
                strtemp += " goodsstate=3 and";
                break;
            case "4"://重点监控
                strtemp += " goodsstate=4 and";
                break;
        }

        //审核状态
        CheckStatus = lstCheckStatus.SelectedValue;
        switch (CheckStatus)
        {
            case "3":  //全部
                break;
            case "0"://未审核
                strtemp += " (SupplyPlan_GoodsNext.PS_ChangeStatus=0 or SupplyPlan_GoodsNext.PS_ChangeStatus is null) and";
                break;
            case "1"://审核通过
                strtemp += " SupplyPlan_GoodsNext.PS_ChangeStatus=1 and";
                break;
            case "2"://审核未通过
                strtemp += " SupplyPlan_GoodsNext.PS_ChangeStatus=2 and";
                break;
        }

        //地区查询
        string  areanameselect = lstAreaName.SelectedValue;
       
        if (areanameselect != "")
        {
            if (areanameselect == "0")
            { }
            else
            {
                strtemp += " areaid = '" + areanameselect + "' and";
            }
        }
       
        if (_ps != "") //按主配送企业查询
        {
            strtemp += " CompanyName_PS like '%" + _ps + "%' and";
        }


        if (_GoodsId != "")
        {
            strtemp += " GoodsId like '%" + _GoodsId + "%' and";

        }


        if (name != "")
        {
            strtemp += " (ProductName like '%" + name + "%'or Product_SpellingCode like '%" + name + "%') and";
        }
        if (outlookc != "")
        {
            strtemp += " outlookc like '%" + outlookc + "%' and";
        }

        if (_companyname_tb != "")
        {
            strtemp += " CompanyName_TB like '%" + _companyname_tb + "%' and";
        }

        if (_usecoder_tb != "")
        {
            strtemp += " UserCode_TB like '%" + _usecoder_tb + "%' and";
        }
        if (_fps!="")
        {
            strtemp += " companyname_ps_b1 like '%" + _fps + "%' and";
        }

        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }

        return strtemp;

    }
    protected void Button1_Click(object sender, EventArgs e)//全部审核童话
    {
        Db db = new Db();
        string strSele = @"update SupplyPlan_Goodsnext set PS_ChangeStatus=1 from SupplyPlan_Goods
                           where SupplyPlan_Goodsnext.SupplyGoodsId=SupplyPlan_Goods.SupplyGoodsId
                          and SupplyPlan_Goodsnext.PS_ChangeStatus<>1 and SupplyPlan_Goodsnext.PS_ChangeStatus<>2 and isuse=1";
        int ii =db.Command(strSele);
        db.Close();
        Response.Write("<script>alert('通过 "+ii+" 条！')</script>");
 
    }
    protected void Button2_Click(object sender, EventArgs e)//未审核
    {
        if (txtps.Text.Trim() != "")
        {
            string strs = @"update SupplyPlan_GoodsNext set PS_ChangeStatus=0 
                            where companyname_ps_M='" + SysFun.ToName(txtps.Text.Trim()) + "' ";
            Db db = new Db();
            int kk = db.Command(strs);
            db.Close();
            Response.Write("<script>alert('影响 " + kk + " 条记录！')</script>");
        }
        else
        {
            Response.Write("<script>alert('主配送企业未填！')</script>");
        }
    }


    protected void Button4_Click(object sender, EventArgs e)//审核通过
    {
        if (txtps.Text.Trim() != "")
        {
            string strs = @"update SupplyPlan_GoodsNext  set PS_ChangeStatus=1 
                            where companyname_ps_M='" + SysFun.ToName(txtps.Text.Trim()) + "' ";
            Db db = new Db();
            int kk = db.Command(strs);
            db.Close();
            Response.Write("<script>alert('影响 " + kk + " 条记录！')</script>");
        }
        else
        {
            Response.Write("<script>alert('主配送企业未填！')</script>");
        }
    }

    protected void Button3_Click(object sender, EventArgs e) //导出
    {
        if (lstAreaName.SelectedValue == "0" || DropDownList1.SelectedValue == "5")
        {
            Response.Write("<script>alert('请选择具体项目和地区！')</script>");
            return;
        }
       Response.Clear();
       Response.Buffer = true;
       Response.Charset = "GB2312";
       Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("审核目录.xls"));
       Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
       Response.ContentEncoding = System.Text.Encoding.UTF7;  //解决将gridview导出成excel时出现乱码  
       Response.ContentType = "application/ms-excel";
       this.EnableViewState = false;
       System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
       System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
       DataSourbandin();
       this.GridView1.RenderControl(oHtmlTextWriter);

       //把文件流发送到客户端 
       Response.Write(oStringWriter.ToString());
       //   停止页面的执行

       Response.End();
     
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //OverRide　为了使导出成Excel可行！
    }
    private void DataSourbandin()
    {
        string Selete = @"SELECT  GoodsId as 流水号,ProductName as 通用名,MedicineModel as 剂型,Outlookc as 规格,Factor as 转换系数,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS 质量层次,UserCode_TB as 中标人注册号,CompanyName_TB as 中标人名称,CompanyName_PS as 主配送企业,areaname as 地区,companyname_ps_B1 as 分配送企业,case GoodsState when '0' then '原中标' when '1' then '自行采购' when '2' then '22种降价' when '3' then '血液制品' when '4' then '重点监控' end as 中标状态,case when SupplyPlan_GoodsNext.PS_ChangeStatus = '0' or SupplyPlan_GoodsNext.PS_ChangeStatus is null then '未审核'   when SupplyPlan_GoodsNext.PS_ChangeStatus ='1' then '审核通过' when SupplyPlan_GoodsNext.PS_ChangeStatus ='2' then '审核未通过' end as 审核状态,SupplyPlan_GoodsNext.Accountname_shr as 审核人 from SupplyPlan_Goods INNER JOIN SupplyPlan_GoodsNext ON SupplyPlan_Goods.SupplyGoodsId = SupplyPlan_GoodsNext.SupplyGoodsId AND SupplyPlan_Goods.AccountId_PS = SupplyPlan_GoodsNext.Accountid_ps_M where " + fctConditions() + "  order by CompanyName_TB,productname,medicinemodel";
        Db db = new Db();
        GridView1.DataSource = db.DataTable(Selete);
        GridView1.DataBind();
        db.Close();
    }

    protected void Button6_Click(object sender, EventArgs e)//导入未审核和表格
    {

    }
}
